package com.cws4cloud.cws.mall.cms.restapi;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.constant.enums.StatusEnum;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.security.annotation.Inner;
import com.cws4cloud.cws.mall.cms.entity.Classlist;
import com.cws4cloud.cws.mall.cms.entity.News;
import com.cws4cloud.cws.mall.cms.service.ClasslistService;
import com.cws4cloud.cws.mall.cms.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: ClassifyRestApi
 * @Description: 分类及新闻
 * @Date 2021/6/28 21:24
 */
@RestController
@RequestMapping("/class")
@Api(value = "新闻栏目接口", tags = {"新闻栏目接口"})
@Slf4j
@Inner
public class ClassifyRestApi {

	@Autowired
	private NewsService newsService;
	@Autowired
	private ClasslistService classlistService;

	@ApiOperation(value = "获取分类详情", notes = "获取分类详情")
	@GetMapping("/getClasslist/{classlistId}")
	public R getClasslistById(@PathVariable("classlistId") Integer classlistId,Integer siteId) {
		Classlist classlist = classlistService.getById(classlistId);
		return R.ok(classlist);
	}

	@ApiOperation(value = "获取新闻详情", notes = "获取新闻详情")
	@GetMapping("/getNews/{newsId}")
	public R getNewsById(@PathVariable("newsId") Integer newsId,Integer siteId) {
		News news = newsService.getById(newsId);
		if (news!=null) {
			News preNews = newsService.getPreNews(news.getClasslistId(),news.getSequence());
			News nextNews = newsService.getNextNews(news.getClasslistId(),news.getSequence());
			news.setPreNews(preNews);
			news.setNextNews(nextNews);
			News updateNews = new News();
			updateNews.setId(newsId);
			updateNews.setClickCount(news.getClickCount() + 1);
			newsService.updateById(updateNews);
		}
		return R.ok(news);
	}

	@ApiOperation(value = "关键字查询新闻", notes = "关键字查询新闻")
	@GetMapping("/getNewsByKeywords")
	public R getNewsByKeywords(@RequestParam(required = true) String keywords,
							   @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
							   @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize,Integer siteId){
		IPage<News> page = newsService.page(new Page<>(currentPage, pageSize), Wrappers.<News>lambdaQuery().like(News::getKeywords,keywords).eq(News::getStatus, StatusEnum.SHOW).eq(News::getSiteId,siteId));
//		List<News> newsList = newsService.list(Wrappers.<News>lambdaQuery().like(News::getKeywords,keywords).eq(News::getSiteId,39));
		return R.ok(page);
	}
}
